package com.ly.mpayment.persist.service.manualorder;

import com.ly.mpayment.core.util.PaginationUtil;
import com.ly.mpayment.persist.entity.ManualOrder;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.math.BigDecimal;

@Service
public class ManualOrderServiceImpl  extends AbstractService<ManualOrder, Serializable> implements IManualOrderService {

    @Override
    public boolean saveManualOrder(ManualOrder manualOrder) {
        if(manualOrder != null){
            this.save(manualOrder);
            return true;
        }
        return false;
    }

    @Override
    public ManualOrder findByUserId(String useruuid, String toShortDate) {
        String sql = "select SUM(m.order_num) as order_num,sum(m.order_money) as order_money from manual_order m where m.useruuid = '"+useruuid+"' " +
                        " and  DATE_FORMAT(m.created_time,'%Y-%m-%d') = '"+toShortDate+"' ";
        Page page = PaginationUtil.queryPaginationSQL(sql,null,this.getSession());
        if(page != null && page.getResult() != null && page.getResult().get(0) != null){
            ManualOrder manualOrder = new ManualOrder();
            Object[] object = (Object[]) page.getResult().get(0);
            manualOrder.setOrderMoney(object[1] == null ? BigDecimal.ZERO : new BigDecimal(object[1].toString()));
            manualOrder.setOrderNum(object[0] == null ? 0 : Integer.parseInt(object[0].toString()));
            return manualOrder;
        }
        return null;
    }
}
